package execute;
import java.util.Vector;
import javax.swing.table.AbstractTableModel;

import knapsackProblem.Item;
import knapsackProblem.KnapsackItem;


@SuppressWarnings("serial")
public class InteractiveTableModel extends AbstractTableModel {
	public static final int ITEM_ID = 0;
	public static final int LOADING_TIME = 1;
	public static final int ITEM_VALUE = 2;
	public static final int HIDDEN_INDEX = 3;

	protected String[] columnNames;
	protected Vector<KnapsackItem> dataVector;

	public Vector<KnapsackItem> getDataVector() {
		return dataVector;		
	}
	
	public InteractiveTableModel(String[] columnNames) {
		this.columnNames = columnNames;
		dataVector = new Vector<KnapsackItem>();
	}

	public String getColumnName(int column) {
		return columnNames[column];
	}

	public boolean isCellEditable(int row, int column) {
		if (column == HIDDEN_INDEX || column == ITEM_ID)
			return false;
		else
			return true;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public Class getColumnClass(int column) {
		switch (column) {
		case ITEM_ID:
		case ITEM_VALUE:
			return Integer.class;
		case LOADING_TIME:
			return Long.class;
		default:
			return Object.class;
		}
	}

	public Object getValueAt(int row, int column) {
		KnapsackItem record = dataVector.get(row);
		switch (column) {
		case ITEM_ID:
			return record.getId();
		case LOADING_TIME:
			return record.getTime();
		case ITEM_VALUE:
			return record.getProfit();
		default:
			return new Object();
		}
	}

	public void setValueAt(Object value, int row, int column) {
		KnapsackItem record = dataVector.get(row);
		switch (column) {
		case ITEM_ID:
			// record.se ((String)value);
			break;
		case LOADING_TIME:
			record.setTime((Long) value);
			break;
		case ITEM_VALUE:
			record.setProfit((Integer) value);
			break;
		default:
			System.out.println("invalid index");
		}
		fireTableCellUpdated(row, column);
	}

	public int getRowCount() {
		return dataVector.size();
	}

	public int getColumnCount() {
		return columnNames.length;
	}

	public boolean hasEmptyRow() {
		if (dataVector.size() == 0)
			return false;
		KnapsackItem audioRecord = dataVector.get(dataVector
				.size() - 1);
		if (audioRecord.getId() == this.getMaxID()
				&& audioRecord.getTime() == 0 && audioRecord.getProfit() == 0) {
			return true;
		} else
			return false;
	}

	public void addEmptyRow() {
		int maxId = this.getMaxID();
		dataVector.add(new KnapsackItem(maxId + 1, 0, 0));
		fireTableRowsInserted(dataVector.size() - 1, dataVector.size() - 1);
	}

	public void addRows(java.util.List<Item> items) {
		if (this.hasEmptyRow()) {
			int lastIdx = dataVector.size() - 1;
			dataVector.remove(lastIdx);
		}

		int maxId = this.getMaxID();
		for (Item item : items) {
			dataVector.add(new KnapsackItem(++maxId, item));
		}
		this.addEmptyRow();
		fireTableRowsInserted(dataVector.size() - 1, dataVector.size() - 1);
	}

	private int getMaxID() {
		int maxId = 0;
		for (int i = 0; i < dataVector.size(); i++) {
			int kiId = dataVector.get(i).getId();
			if (kiId > maxId)
				maxId = kiId;
		}
		return maxId;
	}
}
